<?php

function store_view($custom_columns) {
    global $mysql_jabber,$authorize;
    $username=$authorize->userrow["username"];
    $where=array(
    "username"=>$username,
    );
    $set=array(
    "custom_search"=>serialize($custom_columns),
    );
    $mysql_jabber->change_rows("member",$set,$where);
    }

function prepare_view_nobuttons($rows,$custom_columns) {
    foreach ($custom_columns as $column) {
        $header[]=$column;
        $header_show[]="<table style='padding:1px'><tr><td>".$column."</td></tr></table>";
    }
    foreach ($rows as $row) {
        $data1=array();
        foreach ($header as $hdr) {
            $data1[]=$row[$hdr];
        }
        $data[]=$data1;
    }
    $header_show=array($header_show);
    return array_merge($header_show,$data);
}

function prepare_view($rows,$custom_columns) {
    foreach ($custom_columns as $column) {
        $header[]=$column;
        $header_show[]="<table style='padding:1px'><tr><td>".$column."</td>".column_links("$column")."</tr></table>";
    }
    foreach ($rows as $row) {
        $data1=array();
        foreach ($header as $hdr) {
            $data1[]=$row[$hdr];
        }
        $data[]=$data1;
    }
    $header_show=array($header_show);
    return array_merge($header_show,$data);
}

function column_links($column) {
    global $template_path,$redirect_url,$kriterium,$tip;
    $url=$_SERVER["REQUEST_URI"];
    $form="<form id=\"arrowdown{$column}\" name=\"arrowdown{$column}\" method=\"post\" action=\"{$redirect_url}\">";
    $form.='<input type="hidden" id="arrow" name="arrow" value="arrow">';
    $form.='<input type="hidden" id="point" name="point" value="desc">';
    $form.='<input type="hidden" id="column" name="column" value="'.$column.'">';
    $form.="<input name=\"kriterium\" type=\"hidden\" id=\"txt\" size=\"100\" value=\"{$kriterium}\" />";
    $form.='<input name="o1" type="hidden" id="o1" value="'.$tip.'" />';
    $form.="<input type=\"hidden\" id=\"request\" name=\"request\" value=\"arrowdown\">";
    $arrowdown=$form."<a href='javascript:document.forms[\"arrowdown{$column}\"].submit();'><img width=15px height=15px src='{$template_path}/images/arrow-down.png'></a>";
    $arrowdown.='</form>';

    $form="<form id=\"arrowup{$column}\" name=\"arrowup{$column}\" method=\"post\" action=\"{$redirect_url}\">";
    $form.='<input type="hidden" id="arrow" name="arrow" value="arrow">';
    $form.='<input type="hidden" id="point" name="point" value="asc">';
    $form.='<input type="hidden" id="column" name="column" value="'.$column.'">';
    $form.="<input name=\"kriterium\" type=\"hidden\" id=\"txt\" size=\"100\" value=\"{$kriterium}\" />";
    $form.='<input name="o1" type="hidden" id="o1" value="'.$tip.'" />';
    $form.="<input type=\"hidden\" id=\"request\" name=\"request\" value=\"arrowup\">";
    $arrowup=$form."<a href='javascript:document.forms[\"arrowup{$column}\"].submit();'><img width=15px height=15px src='{$template_path}/images/arrow-up.png'></a>";
    $arrowup.='</form>';
    return "<td>{$arrowup}</td><td>{$arrowdown}</td>";
}

function moderntable($rows,$markheader=false) {
$table="\n<table class='table' width=100%>\n";
if (!is_array($rows) || empty($rows))
	return $table."<tr>\r<td>NEMA REZULTATA!</td>\r</tr>\r</table>\n";

$oddrow=true; $current_row=0;
foreach($rows as $row) {
	$cr=count($row);
	$current_row++;
	if ($oddrow)
    $table.="<tr class='odd'>\n"; else
    $table.="<tr class='even'>\n";
	$columnno=0;
	foreach ($row as $r) {
		$columnno++;
//		if ($columnno<$columnlimit)
        if ($r[0]=="#") {
            $color="bgColor=\"".substr($r,0,7)."\"";
            $r=substr($r,7);
            } else $color="";
			if ($current_row==1 && $markheader)
			$table.="<th class='first'><font size=2><strong>{$r}</strong></font></th>\n"; else
            if ($odd)
			$table.="<td {$color}><font size=2>{$r}</font></td>\n"; else
            $table.="<td {$color}><font size=2>{$r}</font></td>\n";
	}
	$table.="</tr>\n";
	if ($oddrow) $oddrow=false; else $oddrow=true;
}
$table.="</table>\n";
return $table;
}

/**
 * Convert rows to table
 * 
 * @param type $rows
 * @param type $markheader
 * @return string
 */
function rows2table($rows,$markheader=false) {
$table="\n<table class='greentable' width=100%>\n";
if (!is_array($rows) || empty($rows))
	return $table."<tr>\r<td>NEMA REZULTATA!</td>\r</tr>\r</table>\n";

$oddrow=true; $current_row=0;
foreach($rows as $row) {
	$cr=count($row);
	$current_row++;
	$table.="<tr class='greentable'>\n";
	$columnno=0;
	foreach ($row as $r) {
		$columnno++;
//		if ($columnno<$columnlimit)
        if (strstr($r,"#")) {
            $color="bgColor=\"".substr($r,0,7)."\"";
            $r=substr($r,7);
            } else $color="";
			if ($current_row==1 && $markheader)
			$table.="<th class='greentable'>{$r}</th>\n"; else
			$table.="<td class='greentable' {$color}>{$r}</td>\n";
	}
	$table.="</tr>\n";
	if ($oddrow) $oddrow=false; else $oddrow=true;
}
$table.="</table>\n";

return $table;
}

?>